/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics.support;


import java.util.Arrays;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import myarrays.MyArrays;

/**
 *
 * @author asus
 */
public class CreateDataSpectraGraph {

    /**
     *
     * @param data
     * @param working_area_pixel; 2*2 double array; first row represents
     * wavelength in pixels, second row stands for timedelays in pixels.
     * @param X
     * @param Y
     * @param my_xyseriascollection
     * @param add
     * @return
     */
    public XYSeries xyseries;

    private void addtoxyseries(XYSeries xyseries_var, int[][] working_area_pixel, double[] X, double[][] data) {
        for (int i = working_area_pixel[0][0]; i < working_area_pixel[0][1]; i++) {
            xyseries.add(X[i], MyArrays.average(Arrays.copyOfRange(data[i], working_area_pixel[1][0], working_area_pixel[1][1])));
        }
    }

    public XYSeriesCollection createData(double[][] data, int[][] working_area_pixel, double[] X, double[] Y, XYSeriesCollection my_xyseriascollection, Boolean add) {
        XYSeriesCollection xyseriescollection = new XYSeriesCollection();
      
        int size = my_xyseriascollection.getSeriesCount();
        if (add == true) {
            if (size == 1) {
                xyseries = new XYSeries(Integer.toString(size) + ":" + Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s-" + "fixed");

                addtoxyseries(xyseries, working_area_pixel, X, data);

                xyseriescollection.addSeries(xyseries);
                xyseries = new XYSeries(Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s");
                addtoxyseries(xyseries, working_area_pixel, X, data);
                xyseriescollection.addSeries(xyseries);
            } else {
                for (int i = 0; i < size - 1; i++) {
                    xyseriescollection.addSeries(my_xyseriascollection.getSeries(i));
                }
                xyseries = new XYSeries(Integer.toString(size) + ":" + Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s-"+ "fixed");
                addtoxyseries(xyseries, working_area_pixel, X, data);
                xyseriescollection.addSeries(xyseries);
                xyseries = new XYSeries(Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s");
                addtoxyseries(xyseries, working_area_pixel, X, data);
                xyseriescollection.addSeries(xyseries);
            }
        }
        if (add == false) {
            if (size == 1) {
                xyseries = new XYSeries(Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s");
                addtoxyseries(xyseries, working_area_pixel, X, data);
                xyseriescollection.addSeries(xyseries);
            } else {
                for (int i = 0; i < size - 1; i++) {
                    xyseriescollection.addSeries(my_xyseriascollection.getSeries(i));
                }
                xyseries = new XYSeries(Double.toString(X[working_area_pixel[0][0]]) + "-" + Double.toString(X[working_area_pixel[0][1]]) + "nm; " + Double.toString(Y[working_area_pixel[1][0]]) + "-" + Double.toString(Y[working_area_pixel[1][1]]) + "~s");
                addtoxyseries(xyseries, working_area_pixel, X, data);
                xyseriescollection.addSeries(xyseries);
            }
        }
        return xyseriescollection;
    }

}
